<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta name="keywords" content="天地图"/>
     <meta name="viewport" content="height=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
    <title>根据坐标定位企业的具体位置</title>
    <script type="text/javascript" src="http://api.tianditu.com/api?v=4.0"></script>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <style type="text/css">
        body,html{
            width:100%;
            height:100%;
            margin:0;
            font-family:"Microsoft YaHei"
        }
        #mapDiv{
            width:100%;
            height:600px
        }
        .navgation{
            position: absolute;
            right:0;
            top:70%;
            height:50px;
            width:50px;
            font-size:18px;
            color:white;
            padding-top:22px;
            text-align:center;
            background:#2A81CB;
            z-index:1000;
        }
    </style>
    <script>
        var map;
        var zoom = 12;
        var lngLat;
        function onLoad() {
            //初始化地图对象
            map = new T.Map("mapDiv");
            lngLat = getPosition();
            //设置显示地图的中心点和级别
            map.centerAndZoom(lngLat, zoom);
            map.disableDoubleClickZoom();
            //添加覆盖物和提示信息
            //创建标注对象
            var marker = new T.Marker(lngLat);
            //向地图上添加标注
            map.addOverLay(marker);
            getDetailResult(lngLat,marker);
        }

        //逆地理编码查询
        function getDetailResult(lnglat,marker){
            var queryUrl = "http://www.tianditu.com/query.shtml?postStr={'lon':"+lnglat.getLng()+",'lat':"+lnglat.getLat()+",'appkey':8a7b9aac0db21f9dd995e61a14685f05,'ver':1}&type=geocode";
            $.ajax({
                url:queryUrl,
                type:'get',
                dataType:'json',
                success:function(data){
                    console.log(data)
                    if(data.status==0){
                        //创建信息窗口对象
                       var markerInfoWin = new T.InfoWindow(data.result.formatted_address);
                        marker.addEventListener("click", function () {
                            marker.openInfoWindow(markerInfoWin);
                        });// 将标注添加到地图中
                    }
                }
            });
        }

        //获取用户传过来的企业坐标
        function getPosition(){
            if(GetQueryString("lng") &&GetQueryString("lat")){
                return new T.LngLat(GetQueryString("lng"), GetQueryString("lat"));
            }else{
                alert("缺失坐标信息,无法定位");
                window.location.href="http://192.168.50.212/getLocationByPosition.html?lng=116.40969&lat=39.89945"
                // history.go(-1);
            }
        }

        //根据key获取请求字符串中的value
        function GetQueryString(name)
        {
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if(r!=null)return  unescape(r[2]); return null;
        }

        //路线导航
        function navgation(){
           //获取用户当前的位置
           var lo = new T.Geolocation();
           fn = function (e) {
               if (this.getStatus() == 0){
                   map.centerAndZoom(e.lnglat, 15)
                   alert("获取定位坐标："+e.lnglat.lat + "," + e.lnglat.lng)
                   var marker = new T.Marker(e.lnglat);
                   map.addOverLay(marker);

               }
               if(this.getStatus() == 1){
                   map.centerAndZoom(e.lnglat, e.level)
                   alert("获取定位坐标："+e.lnglat.lat + "," + e.lnglat.lng)
                   var marker = new T.Marker(e.lnglat);
                   map.addOverLay(marker);
               }
           }
           lo.getCurrentPosition(fn);
        }
    </script>
</head>
<body onLoad="onLoad()">
<div class="navgation" onclick="navgation()">路线</div>
<div id="mapDiv"></div>
</body>
</html>

